import request from '@/api/request'
import { ResponseFormat, ResponseFormatWithRows } from '@/types/http'

// 动态表单系统 API 接口

// ==================== 表单模板管理 ====================

/**
 * 创建表单模板
 * @param data 表单模板数据
 */
export const createFormTemplate = (data: any): Promise<ResponseFormat> => {
  return request.post('/dynamic-form/template', data)
}

/**
 * 获取表单模板列表
 * @param params 查询参数
 */
export const getFormTemplateList = (params?: {
  page?: number
  limit?: number
  isActive?: boolean
}): Promise<ResponseFormatWithRows> => {
  return request.get('/dynamic-form/template', { params })
}

/**
 * 根据ID获取表单模板
 * @param templateId 模板ID
 */
export const getFormTemplateById = (templateId: string): Promise<ResponseFormat> => {
  return request.get(`/dynamic-form/template/${templateId}`)
}

/**
 * 根据表单标识符获取表单模板
 * @param formKey 表单标识符
 */
export const getFormTemplateByKey = (formKey: string): Promise<ResponseFormat> => {
  return request.get(`/dynamic-form/template/key/${formKey}`)
}

/**
 * 更新表单模板
 * @param templateId 模板ID
 * @param data 更新数据
 */
export const updateFormTemplate = (templateId: string, data: any): Promise<ResponseFormat> => {
  return request.post(`/dynamic-form/template/${templateId}`, data)
}

/**
 * 删除表单模板
 * @param templateId 模板ID
 */
export const deleteFormTemplate = (templateId: string): Promise<ResponseFormat> => {
  return request.delete(`/dynamic-form/template/${templateId}`)
}

// ==================== 字段管理 ====================

/**
 * 向表单模板添加字段
 * @param data 字段数据
 */
export const addFieldToTemplate = (data: {
  formTemplateId: string
  field: any
}): Promise<ResponseFormat> => {
  return request.post('/dynamic-form/template/field', data)
}

/**
 * 从表单模板移除字段
 * @param templateId 模板ID
 * @param fieldName 字段名称
 */
export const removeFieldFromTemplate = (templateId: string, fieldName: string): Promise<ResponseFormat> => {
  return request.delete(`/dynamic-form/template/${templateId}/field/${fieldName}`)
}

// ==================== 表单提交 ====================

/**
 * 提交表单数据
 * @param data 提交数据
 */
export const submitFormData = (data: {
  formTemplateId: string
  formData: any
  submitterName?: string
  submitterEmail?: string
  source?: string
}): Promise<ResponseFormat> => {
  return request.post('/dynamic-form/submit', data)
}

/**
 * 获取表单提交记录列表
 * @param params 查询参数
 */
export const getFormSubmissionList = (params?: {
  page?: number
  limit?: number
  formTemplateId?: string
  status?: string
}): Promise<ResponseFormatWithRows> => {
  return request.get('/dynamic-form/submission', { params })
}

/**
 * 根据ID获取表单提交记录
 * @param submissionId 提交记录ID
 */
export const getFormSubmissionById = (submissionId: string): Promise<ResponseFormat> => {
  return request.get(`/dynamic-form/submission/${submissionId}`)
}

/**
 * 更新提交记录状态
 * @param submissionId 提交记录ID
 * @param data 状态更新数据
 */
export const updateSubmissionStatus = (submissionId: string, data: {
  status: string
  processNote?: string
  processedBy?: string
}): Promise<ResponseFormat> => {
  return request.post(`/dynamic-form/submission/${submissionId}/status`, data)
}

// ==================== 统计信息 ====================

/**
 * 获取表单模板统计信息
 * @param templateId 模板ID
 */
export const getFormTemplateStatistics = (templateId: string): Promise<ResponseFormat> => {
  return request.get(`/dynamic-form/statistics/template/${templateId}`)
}

/**
 * 获取动态表单系统概览统计
 */
export const getDynamicFormOverviewStatistics = (): Promise<ResponseFormat> => {
  return request.get('/dynamic-form/statistics/overview')
}
